<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;

use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DepartmentController extends Controller
{
    function __construct(Request $req){
		
		$session = $req->session()->get('member');
		if($session['gid'] != 1) abort(403,'Unauthorized action.');
	}
	
	function index(){

		return view('backend.department.index');
	}
	
	/* 公司设置 / 组织与员工管理 */
	function department(){
		echo route('backend.comset.department',['id'=>1]);
	}
	
	function show(Request $request, $id){
		// echo '<pre>';print_R($request);
		echo $request->path();
		echo '<br>';
		echo $request->url();
		echo '<br>';
		echo $request->fullUrl();
		echo '<pre>';print_R($_SERVER);
		// $array = array(2,6,3,9,8,5,7);
		// echo $id;
		// print_R($_GET);
	}
	
	//部门列表ajax
	function departmentlist(){
	
		$deptsTree = $depts = $temp = [];
		
	 	$department = DB::table('department')->select('*', 'fid as pid', 'department_id as id')->get();
		
		foreach($department as $dt){

			$depts[] = (array) $dt;
			$temp[$dt->fid][] = (array) $dt;
		}

		$deptsTree = $this->createTree($temp[0], $temp);
		$json = ['depts'=>$depts, 'deptsTree'=> $deptsTree];
		
		// echo '<pre>';print_R($json);exit;
		echo json_encode($json);
	}
	
	function createTree($menu, $temp){
		
		$data = $menu;
		foreach($menu as $k => $m){
			if( isset($temp[$m['id']]) )
				$data[$k]['childDepartment'] = $this->createTree($temp[$m['id']], $temp );
			else
				$data[$k]['childDepartment'] = [];
		}
		
		return $data;
	}
	
	function store(Request $request){
		
		$form = $request->all();
		if($form['id']){
			//编辑
			print_R($form);
			DB::table('department')->where('department_id' , $form['id'])->update( ['name' => $form['name']]);
		}else{
			//插入
			$data = [
				'name' => $form['name'],
				'fid' => $form['pid']
			];
			DB::table('department')->insert($data);
		}
		echo 1;
	}
	
	//destroy
	function destroy($id){

		DB::delete('delete from xz_department where department_id='. $id);
	}
	
	
	// 获取部门人员
	function getMember($id){

		$member = $tmpArr = $temp = $dName = [];
		
		//部门列表 
		$department = DB::table('department')->get();
		foreach($department as $dt){
			$temp[$dt->fid]['id'] = $dt->department_id;
			// 部门名称数组
			$dName[$dt->department_id] = $dt->name;
		}
		$dName[0] = '';
		
		
		if($id == 0){
			$tmpArr = DB::table('member')->get();
		}else{
			
			//所属部门及下属人员
			$dt_ids = $this->getDepertmentId($id, $temp);
			$dt_ids[] = $id; 
			
			$tmpArr = DB::table('member')->whereIn('department_id', $dt_ids)->get();
		}
		
		foreach($tmpArr as $k =>$t){
			$member[$k] = (array) $t;
			$member[$k]['department_name'] = $dName[$t->department_id];
		}
		
		echo json_encode($member);
		// echo '<pre>'; print_R($member);
	}
	
	function getDepertmentId($id, $temp){
		$data = [];
		if( isset($temp[$id]) ){
			foreach($temp[$id] as $k => $t){
				$data[] = $t;
				$ids = $this->getDepertmentId($t, $temp);
				$data = array_merge($data, $ids);
			}
		}
		return $data;
	}
}
